package com.idconfirmsystem.activity;

import java.io.IOException;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.utils.KJLoger;
import org.xmlpull.v1.XmlPullParserException;

import com.idconfirmsystem.Conf;
import com.idconfirmsystem.Constants;
import com.idconfirmsystem.R;
import com.idconfirmsystem.R.color;
import com.idconfirmsystem.dialog.DialogHelper;
import com.idconfirmsystem.handler.LoginHandler;
import com.idconfirmsystem.handler.ModifyPasswordHandler;
import com.idconfirmsystem.model.UserBean;
import com.idconfirmsystem.parse.LoginParse;
import com.idconfirmsystem.util.CommonHelper;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ModifyPasswordActivity extends Activity {

	private static final String TAG = "ModifyPasswordActivity";
	// handler
	private Handler handler = new ModifyPasswordHandler(this);
	// 布局元素
	private Element el = new Element();

	private class Element {
		private EditText username;
		private EditText password;
		private Button loginBtn;
		private Button cancelBtn;
		private TextView register;
		private Dialog cancelDialog;
		private RadioGroup accountType;
		private RadioButton defend;// 守押用户
		private RadioButton customer;// 客户用户
		public DialogHelper displayMsg = new DialogHelper(
				ModifyPasswordActivity.this, handler);
	}

	// 运行时临时变量
	private RuntimeObject ru = new RuntimeObject();

	private class RuntimeObject {
	}

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_modify_password);

		//initView();
	}

	public void initView() {
		el.username = (EditText) findViewById(R.id.username_edit);
		el.username.setText("90129");
		el.password = (EditText) findViewById(R.id.password_edit);
		el.password.setText("123456");
		el.accountType = (RadioGroup) findViewById(R.id.customer_radio);
		el.defend = (RadioButton) findViewById(R.id.radio_one);
		el.customer = (RadioButton) findViewById(R.id.radio_two);
		el.loginBtn = (Button) findViewById(R.id.signin_button);
		el.loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				handler.sendMessage(handler
						.obtainMessage(Constants.Login.LOGIN));
				// handler.sendMessage(handler
				// .obtainMessage(Constants.MsgWhat.GO_MENU_CONTROL_ACTIVITY));
				// login();
				// soaplogin();
			}
		});
		el.register = (TextView) findViewById(R.id.register_link);
		el.register.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Intent intent = new Intent();
				// intent.setClass(LoginControlActivity.this,
				// RegisterControlActivity.class);
				// startActivity(intent);
				Toast.makeText(ModifyPasswordActivity.this, "功能暂未开放，请联系管理员！",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public void login() {
		// 判别用户名、密码
		String name, psw;
		name = el.username.getText().toString();
		psw = el.password.getText().toString();
		if (name == null || name.trim().length() == 0) {
			showMessage("用户名不能为空");
			return;
		} else if (psw == null || psw.trim().length() == 0) {
			showMessage("密码不能为空");
			return;
		}
		UserBean.getInstance().setUserName(name);
		UserBean.getInstance().setPassword(psw);
		// 识别用户类型
		int checkedRadio = el.accountType.getCheckedRadioButtonId();
		switch (checkedRadio) {
		case R.id.radio_one:
			Conf.LoginConf.ACCOUNT_TYPE = 1;
			Conf.LoginConf.ACCOUNT_TYPE_NAME = "守押用户";
			UserBean.getInstance().setType("SY");
			break;
		case R.id.radio_two:
			Conf.LoginConf.ACCOUNT_TYPE = 2;
			Conf.LoginConf.ACCOUNT_TYPE_NAME = "客户用户";
			UserBean.getInstance().setType("KH");
			break;
		default:
			break;
		}
		CommonHelper.Msg.sendMsg(handler, Constants.Login.DIALOG_SHOW,
				"正在登录,请稍候...");
//		try {
//			soaplogin(name, psw, UserBean.getInstance().getType());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		// CommonHelper.Msg.sendMsg(handler, Constants.Login.DIALOG_DISMISS);
	}

	public void updatePwd() {

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// 命名空间
				String nameSpace = "zyznkj";
				// 调用方法的名称
				String methodName = "updatePwd";
				// EndPoint
				String endPoint = "http://103.27.4.51:8080/syws/services/LoginCheckService.LoginCheckServiceHttpSoap11Endpoint/";
				// SOAP Action
				String soapAction = "http://103.27.4.51:8080/syws/services/LoginCheckService/";
				// 指定WebService的命名空间和调用方法
				SoapObject soapObject = new SoapObject(nameSpace, methodName);
				// 设置需要调用WebService接口的两个参数mobileCode UserId
				soapObject.addProperty("userid", "60302");
				soapObject.addProperty("oldPwd", "123456");
				soapObject.addProperty("newPwd", "123456");
				soapObject.addProperty("mobileNumber", "13612345677");
				soapObject.addProperty("type", "SY");
				// 生成调用WebService方法调用的soap信息，并且指定Soap版本
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER10);
				envelope.bodyOut = soapObject;
				// 是否调用DotNet开发的WebService
				envelope.dotNet = true;
				envelope.setOutputSoapObject(soapObject);
				HttpTransportSE transport = new HttpTransportSE(endPoint);
				try {
					transport.call(soapAction, envelope);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// 获取返回的数据
				SoapObject object = (SoapObject) envelope.bodyIn;
				// 获取返回的结果
				String result = object.getProperty(0).toString();
				Log.e("updatePwd", result);
			}
		}).start();

	}

	public void showMessage(String text) {
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		KJLoger.debug(text);
	}

	public void dialogDisplay(String str) {
		el.displayMsg.displayMessage("信息", str);
	}

	public void dialogDismiss() {
		el.displayMsg.dismiss();
	}

	public void newAlertDialog(String title, String message) {
		new AlertDialog.Builder(ModifyPasswordActivity.this).setTitle(title)
				.setMessage(message).setPositiveButton("确定", null).create()
				.show();
	}
	/*
	 * 利用KJHttp进行数据的获取 KJHttp kjp = new KJHttp(); HttpParams kParams = new
	 * HttpParams(); kParams.put("userid", "60302"); kParams.put("pwd",
	 * "123456"); kParams.put("mobileNumber", "13612345677");
	 * kParams.put("type", "SY"); String url =
	 * "http://103.27.4.51:8080/syws/services/LoginCheckService/loginCheck";
	 * kjp.get(url, kParams, new HttpCallBack() {
	 * 
	 * @Override public void onFailure(int errorNo, String strMsg) { // TODO
	 * Auto-generated method stub super.onFailure(errorNo, strMsg);
	 * toast(strMsg); }
	 * 
	 * @Override public void onSuccess(Map<String, String> headers, byte[] t) {
	 * // TODO Auto-generated method stub super.onSuccess(headers, t); //
	 * System.err.println(headers); // Log.i("kymjs", new String(t)); }
	 * 
	 * @Override public void onSuccess(String t) { // TODO Auto-generated method
	 * stub super.onSuccess(t); Log.i("kymjs", new String(t)); toast(t); t =
	 * t.replace("&lt;", "<"); int bgindx = t.indexOf("<?xml"); int endIndex =
	 * t.lastIndexOf("</ns:return>"); String s2 = t.substring(bgindx, endIndex);
	 * System.err.println(s2); XStream st = new XStream(); // st.fromXML(t); }
	 * 
	 * });
	 */
}
